package com.whq.knowverywell.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.whq.knowverywell.R;
import com.whq.knowverywell.business.model.BaseSalesRecord;
import com.whq.knowverywell.business.model.RegionSalesRecord;
import com.whq.knowverywell.business.model.StoreSalesRecord;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.StringUtil;
import com.whq.knowverywell.util.constant.ModelConstant;

/**
 * content list adapter
 * 
 * @author daiwei
 * 
 */
public class ContentListAdapter extends BaseAdapter {
    private static final String TAG = ContentListAdapter.class.getSimpleName();

    private LayoutInflater inflater;
    private Activity ctx;
    private List resultList;
    private int type;
    
    private int durationType;

    public ContentListAdapter(Activity ctx, List resultList, int type, int durationType) {
        this.type = type;
        this.durationType = durationType;
        this.resultList = resultList;
        this.ctx = ctx;

        try {
            this.inflater = LayoutInflater.from(ctx);
        } catch (Exception e) {
            LogUtil.e(TAG, "exception:", e);
        }
        if (this.resultList == null) {
            this.resultList = new ArrayList();
        }
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public Object getItem(int position) {
        return resultList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SummaryViewHolder huizongViewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.huizong_item, null);
            huizongViewHolder = new SummaryViewHolder();
            huizongViewHolder.mallTextView = (TextView) convertView.findViewById(R.id.mall_textview);
            huizongViewHolder.sellAmountTextView = (TextView) convertView.findViewById(R.id.sell_amount_textview);
            huizongViewHolder.sellCountTextView = (TextView) convertView.findViewById(R.id.sell_count_textview);
            huizongViewHolder.synchronousSellAmountTextView = (TextView) convertView
                    .findViewById(R.id.synchronous_sell_amount_textview);
            huizongViewHolder.synchronousSellCountTextView = (TextView) convertView
                    .findViewById(R.id.synchronous_sell_count_textview);

            convertView.setTag(huizongViewHolder);
        } else {
            huizongViewHolder = (SummaryViewHolder) convertView.getTag();
        }

        LogUtil.i(TAG, "businessType: " + type);
        
        switch (type) {
        case ModelConstant.BUSINESS_TYPE_SUMMARY:
            StoreSalesRecord record = (StoreSalesRecord) getItem(position);
            if (record == null) {
                LogUtil.e(TAG, "the position[" + position + "] item is null .");
                return convertView;
            }

            huizongViewHolder.mallTextView.setText(record.getName());
            huizongViewHolder.sellAmountTextView.setText("￥ " + StringUtil.formatAmount(record.getSalesAmount()));
            huizongViewHolder.sellCountTextView.setText(record.getSalesVolume() + "");
            
            if ( (this.durationType == ModelConstant.DURATION_TYPE_YESTERDAY) 
            		|| (this.durationType == ModelConstant.DURATION_TYPE_TODAY)  
            		) {
            	huizongViewHolder.synchronousSellAmountTextView.setVisibility(View.GONE);
                huizongViewHolder.synchronousSellCountTextView.setVisibility(View.GONE);
            } else {
            	huizongViewHolder.synchronousSellAmountTextView.setVisibility(View.VISIBLE);
                huizongViewHolder.synchronousSellCountTextView.setVisibility(View.VISIBLE);
                
                huizongViewHolder.synchronousSellAmountTextView.setText(StringUtil.formatAmount(record.getLastTermSalesAmount()));
                huizongViewHolder.synchronousSellCountTextView.setText(record.getLastTermSalesVolume() + "");
            }
            break;

        case ModelConstant.BUSINESS_TYPE_DOMAIN:
        	RegionSalesRecord domainRecord = (RegionSalesRecord) getItem(position);
            if (domainRecord == null) {
                LogUtil.e(TAG, "the position[" + position + "] item is null .");
                return convertView;
            }

            huizongViewHolder.mallTextView.setText(domainRecord.getName());
            huizongViewHolder.sellAmountTextView.setText("￥ " + StringUtil.formatAmount(domainRecord.getSalesAmount()));
            huizongViewHolder.sellCountTextView.setText(domainRecord.getSalesVolume() + "");
            
            if ( (this.durationType == ModelConstant.DURATION_TYPE_YESTERDAY) 
            		|| (this.durationType == ModelConstant.DURATION_TYPE_TODAY)  
            		) {
            	huizongViewHolder.synchronousSellAmountTextView.setVisibility(View.GONE);
                huizongViewHolder.synchronousSellCountTextView.setVisibility(View.GONE);
            } else {
            	huizongViewHolder.synchronousSellAmountTextView.setVisibility(View.VISIBLE);
                huizongViewHolder.synchronousSellCountTextView.setVisibility(View.VISIBLE);
                
                huizongViewHolder.synchronousSellAmountTextView.setText(StringUtil.formatAmount(domainRecord.getLastTermSalesAmount()));
                huizongViewHolder.synchronousSellCountTextView.setText(domainRecord.getLastTermSalesVolume() + "");
            }
            
            break;

        case ModelConstant.BUSINESS_TYPE_SORT:
            BaseSalesRecord sortRecord = (BaseSalesRecord) getItem(position);
            if (sortRecord == null) {
                LogUtil.e(TAG, "the position[" + position + "] item is null .");
                return convertView;
            }

            huizongViewHolder.mallTextView.setText(sortRecord.getName());
            huizongViewHolder.sellAmountTextView.setText("￥ " + StringUtil.formatAmount(sortRecord.getSalesAmount()));
            huizongViewHolder.sellCountTextView.setText(sortRecord.getSalesVolume() + "");
            huizongViewHolder.synchronousSellAmountTextView.setVisibility(View.GONE);
            huizongViewHolder.synchronousSellCountTextView.setVisibility(View.GONE);

            break;

        default:
            break;
        }

        return convertView;
    }

    class SummaryViewHolder {
        TextView mallTextView, sellAmountTextView, synchronousSellAmountTextView, sellCountTextView,
                synchronousSellCountTextView;
    }
}
